<template>
  <div class="execution">
      <el-form :inline="true" :model="dataForm" size="mini" v-if="searchFormShow">
        <el-row class="bg-row">
          <el-col :span="6">
            <el-form-item label="所属政府">
              <el-select v-model="dataForm.governmentId">
                <el-option v-for="item in governmentOptions" :value="item.id" :label="item.governmentName" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门名称">
              <el-input v-model="dataForm.departmentName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button size="mini" icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
              <el-button size="mini" icon="el-icon-delete" type="info" @click="clearSearchForm">清空</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="margin-top: 5px;margin-bottom: 5px;">
        <el-row style="margin-top: 5px;">
          <el-button v-if="permissions.admin_department_add" size="mini" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle('','add')">新增</el-button>
          <el-tooltip class="item" effect="dark" content="搜索" placement="top"style="float: right;">
            <el-button icon="el-icon-search" circle @click="searchFormShow = !searchFormShow"></el-button>
          </el-tooltip>
        </el-row>
      </div>

      <el-table :data="dataList" height="calc(100vh - 215px)" border stripe v-loading="dataListLoading">
          <el-table-column prop="id" label="部门编号" min-width="100"></el-table-column>
          <el-table-column prop="departmentName" label="部门名称" min-width="120"></el-table-column>
          <el-table-column prop="governmentId" label="政府名称" min-width="180">
            <template slot-scope="scope">
              <span>{{statusFormat(scope.row.governmentId)}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="responsibility" label="部门职责" min-width="180"></el-table-column>
          <el-table-column label="操作" width="230" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-view" @click="addOrUpdateHandle(scope.row.id,'check')">详情</el-button>
              <el-button v-if="permissions.admin_department_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id,'edit')">编辑</el-button>
              <el-button v-if="permissions.admin_department_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row)">删除</el-button>
            </template>
          </el-table-column>

        </el-table>

      <div class="avue-crud__pagination" style="margin-bottom: 25px;">
        <el-pagination
					@size-change="sizeChangeHandle"
					@current-change="currentChangeHandle"
					:current-page="pageIndex"
					:page-sizes="[10, 20, 50, 100, 200]"
					:page-size="pageSize"
					:total="totalPage"
					background
					layout="total, sizes, prev, pager, next">
        </el-pagination>
      </div>

      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
  </div>
</template>

<script>
  import { filterParams } from '@/util/tools'
  import { fetchList, delObj } from '@/api/pvm/department'
  import { getGovernmentList } from '@/api/pvm/government'
  import { mapGetters } from 'vuex'

  import TableForm from './TableForm'

  export default {
    name: 'pvmdepartment',
    data() {
      return {
        searchFormShow: true,
        dataList: [],
        dataListLoading: false,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataForm: {},
        governmentOptions: [],
        addOrUpdateVisible: false,
      }
    },
    components: {
      TableForm
    },
    computed: {
      ...mapGetters(['permissions'])
    },

    created() {
      this.getGovernmentList()
      this.searchHandle()
    },
    methods: {
      // 查询数据
      searchHandle(){
        this.pageIndex = 1
        this.getDataList()
      },
      // 清空查询表单
      clearSearchForm(){
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
      },
      // 获取数据列表
      getDataList(){
        let params1 = {current: this.pageIndex,size: this.pageSize}
        let params2 = filterParams(this.dataForm)
        this.dataListLoading = true
        fetchList(Object.assign(params1,params2)).then(response => {
					if(response.data.code == '0'){
					  this.dataList = response.data.data.records
					  this.totalPage = response.data.data.total
					}else{
					  this.$message.error(response.data.mdg)
					}
          this.dataListLoading = false
        })
      },
      // 每页条数改变
      sizeChangeHandle(val){
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页改变
      currentChangeHandle(val){
        this.pageIndex = val
        this.getDataList()
      },
      // 打开弹窗 新增 / 修改 / 详情
      addOrUpdateHandle(id,type){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,type)
        })

      },
      // 删除
      deleteHandle(row){
        this.$confirm('此操作将永久删除该部门(部门名为:' + row.departmentName + '), 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delObj(row.id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.mdg)
          }
        })
      },

      //  转换类型格式
      statusFormat(val){
        let obj = {}
        obj = this.governmentOptions.find(item => {
          if(item.id == val){
            return item
          }
        })
        if(obj && obj.governmentName){
          return obj.governmentName
        }
      },
      // 获取政府列表
      getGovernmentList(){
        getGovernmentList().then(response => {
          if(response.data.code == '0'){
            this.governmentOptions = response.data.data
          }else{
            this.$message.error(response.data.mdg)
          }
        })
      }
    }
  }
</script>
